//
//  FirCirPersonDataSource.m
//  tcpoctest
//
//  Created by gump on 5/6/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "FirCirPersonDataSource.h"

#import "FirCirNewsDataSource.h"

@implementation FirCirPersonDataSource

-(id)init
{
    self = [super init];
    
    if(self)
    {
        fcpersonDic = [[NSMutableDictionary alloc] init];
        pthread_mutex_init(&mutexfcperson,0);
        self.curshowarr = nil;
        deleteIDArr = [[NSMutableSet alloc] init];
    }
    
    return self;
}

-(void)insertNewsIDToMail:(NSString *)mail withNewsID:(NSString *)newid withOrder:(int)io
{
    pthread_mutex_lock(&mutexfcperson);
    
    NSMutableArray  *arr = [fcpersonDic objectForKey:mail];
    if (arr == nil) {
        arr = [[NSMutableArray alloc] init];
        [fcpersonDic setObject:arr forKey:mail];
    }
    
    if (![arr containsObject:newid]) {
        if (io == ORDER_ASC) {
            [arr addObject:newid];
        }
        else if (io == ORDER_DES)
        {
            [arr insertObject:newid atIndex:0];
        }
    }
    
    pthread_mutex_unlock(&mutexfcperson);
}

-(NSMutableArray *)getShowArrByMail:(NSString *)mail
{
    NSMutableArray *arr = nil;
    
    pthread_mutex_lock(&mutexfcperson);
    
    arr = [fcpersonDic objectForKey:mail];
    if (arr == nil) {
        arr = [[NSMutableArray alloc] init];
        [fcpersonDic setObject:arr forKey:mail];
    }
    
    pthread_mutex_unlock(&mutexfcperson);
    
    return arr;
}

-(void)setCurshow:(NSMutableArray *)arr
{
    pthread_mutex_lock(&mutexfcperson);
    
    self.curshowarr = arr;
    
    pthread_mutex_unlock(&mutexfcperson);
}

-(NSMutableArray *)Curshow
{
    return self.curshowarr;
}

-(void)DelAllDic
{
    pthread_mutex_lock(&mutexfcperson);
    
    [fcpersonDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexfcperson);
}

-(void)SetIDLength:(int)ilength
{
    iNewIdLength = ilength;
}

-(int)IDLength
{
    return iNewIdLength;
}

-(void)SetYetLength:(int)ilength
{
    iYetRead = ilength;
}

-(int)YetLength
{
    return iYetRead;
}

-(NSString *)CurShowObjectByIndex:(int)index
{
    NSString *str = @"";
    
    pthread_mutex_lock(&mutexfcperson);
    
    if (self.curshowarr != nil &&
        index >= 0 &&
        index < self.curshowarr.count) {
        str  =  [self.curshowarr objectAtIndex:index];
    }
    
    pthread_mutex_unlock(&mutexfcperson);
    
    return str;
}

-(void)SortCurShow
{
    pthread_mutex_lock(&mutexfcperson);
    

    if (self.curshowarr != nil) {
        self.curshowarr = [NSMutableArray arrayWithArray:
        [self.curshowarr sortedArrayUsingFunction:customSortFCP context:nil]];
    }
    
    pthread_mutex_unlock(&mutexfcperson);
}

NSInteger customSortFCP(id obj1, id obj2,void* context)
{
    NSString* obj_1 = (NSString*)obj1;
    NSString* obj_2 = (NSString*)obj2;
    
    NSComparisonResult res = (NSComparisonResult)NSOrderedSame;
    
    do {
        
        FriendStatus *fs1 = [[DataControl shareControl].fcnewsDS getNewsByID:obj_1];
        FriendStatus *fs2 = [[DataControl shareControl].fcnewsDS getNewsByID:obj_2];
        
        if (fs1 == nil || fs2 == nil) {
            break;
        }
        
        NSArray *array = [fs1.strtime componentsSeparatedByString:@"/"];
        NSArray *array1 = [fs2.strtime componentsSeparatedByString:@"/"];
        
        if ([array count] != 6 || [array1 count] != 6) {
            break;
        }
        
        for(int i = 0 ; i < 6 ; ++i)
        {
            int idate = [[array objectAtIndex:i] intValue];
            int idate1 = [[array1 objectAtIndex:i] intValue];
            
            if(idate > idate1)
            {
                res = (NSComparisonResult)NSOrderedAscending;
                break;
            }
            else if(idate < idate1)
            {
                res = (NSComparisonResult)NSOrderedDescending;
                break;
            }
        }
        
    } while (false);
    
    return res;
}

-(void)DelCurshowArrObject:(NSString *)strid
{
    pthread_mutex_lock(&mutexfcperson);
    
    if (self.curshowarr != nil) {
        [self.curshowarr removeObject:strid];
    }
    
    NSMutableArray *arr = [fcpersonDic objectForKey:
                           [DataControl shareControl].personDS.uinfo.mail];
    if (arr != nil) {
        [arr removeObject:strid];
    }
    
    pthread_mutex_unlock(&mutexfcperson);
}

-(void)AddDelIDArr:(NSString *)strid
{
    pthread_mutex_lock(&mutexfcperson);
    
    if (![deleteIDArr containsObject:strid]) {
        [deleteIDArr addObject:strid];
    }
    
    pthread_mutex_unlock(&mutexfcperson);
}

-(void)RemoveAllDelArr
{
    pthread_mutex_lock(&mutexfcperson);
    
    [deleteIDArr removeAllObjects];
    
    pthread_mutex_unlock(&mutexfcperson);
}

-(BOOL)IsContainInDelArr:(NSString *)strid
{
    BOOL bcontain = NO;
    
    pthread_mutex_lock(&mutexfcperson);
    
    bcontain = [deleteIDArr containsObject:strid];
    
    pthread_mutex_unlock(&mutexfcperson);
    
    return bcontain;
}

@end
